//
//  MainPageViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  首页

import UIKit
import WebKit


class MainPageViewController : WKBaseViewController {
    //token
    
    //设置导航栏样式
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        
        //若导航栏的子页面数量大于1，则设置第一面的导航栏颜色为白色，其他为橘色
        if (self.navigationController?.viewControllers.count)! > 1 {
            self.navigationController?.navigationBar.barTintColor = UIColor.white
            //文字颜色
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.black]
            
            UIApplication.shared.statusBarStyle = .default
        } else {
            
            self.navigationController?.navigationBar.barTintColor = commonBtnColor
            //文字颜色
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
            
            UIApplication.shared.statusBarStyle = .lightContent
            
        }
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "首页"
        
        //从存跳转链接的数组中获取最后一个链接，最后当前页面加载的链接
        if mainIndexArray.count > 1 {
            //取出数组的url转为字符串
            url = mainIndexArray.lastObject as! String
            
            webView.load(URLRequest.init(url: URL.init(string: url)!))
        } else {
            DispatchQueue.main.async {
                
                let urlStr = "http://\(yuMing)/app/index.aspx?devtype=1" + "&token=" + self.token
                
                //判断是否包含域名
                if urlStr.contains(comStrURL) {
                    let url1 = URL.init(string: urlStr)
                    
                    //根据是否有网络状态加载断网图片
                    StatusBarCheck.shared.networkingStatesFromStatebar { (result) in
                        
                        if result == "notReachable" {
                            DispatchQueue.main.async {
                                
                                self.urlRequestCache = NSURLRequest.init(url: url1!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                                
                                self.webView.load(self.urlRequestCache as URLRequest)
                                
                            }
                            
                        } else {
                            DispatchQueue.main.async {
                                
                                self.urlRequestCache = NSURLRequest(url: url1!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                                
                                self.webView.load(self.urlRequestCache as URLRequest)
                                
                            }
                        }
                    }
                    
                } else {
                    CustomAlertView.shared.alertWithTitle(strTitle: "请下拉刷新")
                }
            }
        }
    }
    
    //拦截操作
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        textView.text = self.url
        
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            
            //XFLog(message: mainIndexArray)
            
            if self.url.contains("?") {
                self.url = self.url + ("&devtype=1&token=") + (token)
            } else {
                self.url = self.url + ("?devtype=1&token=") + (token)
            }

            mainIndexArray.add(self.url)
            self.aaa(str: mainIndexArray.lastObject as! String)
            
            decisionHandler(.cancel)
            
        } else {
            decisionHandler(.allow)
        }
        
    }
    
    
    
    /// 页面跳转
    ///
    /// - Parameter str: 跳转的链接
    func aaa(str : String) -> Void {
        
        let vvv = MainPageViewReplaceVIew()
        vvv.url = str
        
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
        self.hidesBottomBarWhenPushed = false
    }
}

